//
//  MineCommunityTableViewCell.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/8.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "MineCommunityTableViewCell.h"
#import "LMJElementsFlowLayout.h"
#import "ChatroomModel.h"
static NSString*const XCommunityClvCellReuseIdentifier = @"XCommunityClvCellReuseIdentifier";
@interface MineCommunityTableViewCell()<LMJElementsFlowLayoutDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView*collectionView;
@end

@implementation MineCommunityTableViewCell

- (void)setData:(NSArray <ChatroomModel*>*)data{
    _data = data;
    [self.collectionView reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setupUI{
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.bottom.mas_equalTo(0);
    }];
    [self.collectionView registerClass:XCommunityClvCell.class forCellWithReuseIdentifier:XCommunityClvCellReuseIdentifier];
}

#pragma mark - UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XCommunityClvCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:XCommunityClvCellReuseIdentifier forIndexPath:indexPath];
    ChatroomModel*chat = [self.data objectAtIndex:indexPath.item];
    NSMutableAttributedString *mutablAttrStr = [[NSMutableAttributedString alloc]init];
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    attch.image = [UIImage imageNamed:@"number_of_person"];
    attch.bounds = CGRectMake(0, -1, attch.image.size.width, attch.image.size.height);
    //创建带有图片的富文本
    [mutablAttrStr insertAttributedString:[NSAttributedString attributedStringWithAttachment:attch] atIndex:0];
    [mutablAttrStr appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",chat.user_num]]];
    cell.numLab.attributedText =  mutablAttrStr;
    
    cell.titleLab.text = chat.name;
    [cell.iconV sd_setImageWithURL:String_To_URL(chat.logo)];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    !self.ClickBlock?:self.ClickBlock([self.data objectAtIndex:indexPath.item]);
}
#pragma mark - LMJElementsFlowLayoutDelegate

- (CGSize)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kCommunityClvCellWidth, kCommunityClvCellHeight);
}

- (UIEdgeInsets)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout edgeInsetsInCollectionView:(UICollectionView *)collectionView
{
    return UIEdgeInsetsMake(0,0,0,0);
}

- (CGFloat)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView columnsMarginForItemAtIndexPath:(NSIndexPath *)indexPath{
    return 0;
}

- (CGFloat)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView linesMarginForItemAtIndexPath:(NSIndexPath *)indexPath{
    return 0;
}

- (UICollectionView*)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:[LMJElementsFlowLayout flowLayoutWithDelegate:self]];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


@implementation XCommunityClvCell

- (void)setupUI{
//    number_of_person
    self.contentView.backgroundColor = UIColor.clearColor;
    self.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:self.iconV];
    [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(50.0f), AdaptedWidth(50.0f)));
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(kNormalSpace);
    }];
    [self.contentView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconV.mas_right).offset(AdaptedWidth(10));
        make.right.mas_equalTo(AdaptedWidth(-5));
        make.top.equalTo(self.iconV.mas_top).offset(AdaptedWidth(6));
    }];
    [self.contentView addSubview:self.numLab];
    [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconV.mas_right).offset(AdaptedWidth(10));
        make.bottom.equalTo(self.iconV.mas_bottom).offset(AdaptedWidth(-6));
    }];
}
- (UIImageView*)iconV{
    if (!_iconV) {
        _iconV = [UIImageView new];
        ViewBorderRadius(_iconV, AdaptedWidth(13), 1, kLineColor);
    }
    return _iconV;
}

- (UILabel*)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.textColor = UIColor.blackColor;
        _titleLab.font = kBoldFont(16);
    }
    return _titleLab;
}

- (UILabel*)numLab{
    if (!_numLab) {
        _numLab = [UILabel new];
        _numLab.textColor = UIColorHex(#999999);
        _numLab.font = kDefaultFont(12);
    }
    return _numLab;
}

@end
